<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha256-xejo6yLi6vGtAjcMIsY8BHdKsLg7QynVlFMzdQgUuy8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"gz1234.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.3","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"flat"},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="git 命令   命令 描述    git init git仓库初始化   git clone  复制现有仓库   git status 查看git仓库状态   git log 查看commit列表记录–oneline  浓缩成一行一条记录–stat         输出文件改动的统计数据-p               输出改动文件的具体内容-w              忽略空格的修改和-p一">
<meta property="og:type" content="article">
<meta property="og:title" content="git常用命令">
<meta property="og:url" content="https://gz1234.gitee.io/2021/12/12/git/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/index.html">
<meta property="og:site_name" content="郭泽">
<meta property="og:description" content="git 命令   命令 描述    git init git仓库初始化   git clone  复制现有仓库   git status 查看git仓库状态   git log 查看commit列表记录–oneline  浓缩成一行一条记录–stat         输出文件改动的统计数据-p               输出改动文件的具体内容-w              忽略空格的修改和-p一">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-12-12T14:37:05.000Z">
<meta property="article:modified_time" content="2023-03-22T01:11:36.518Z">
<meta property="article:author" content="郭泽">
<meta property="article:tag" content="git命令">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gz1234.gitee.io/2021/12/12/git/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://gz1234.gitee.io/2021/12/12/git/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/","path":"2021/12/12/git/git常用命令/","title":"git常用命令"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>git常用命令 | 郭泽</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">郭泽</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#git-%E5%91%BD%E4%BB%A4"><span class="nav-number">1.</span> <span class="nav-text">git 命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git%E6%81%A2%E5%A4%8D%E6%9F%90%E4%B8%AA%E6%96%87%E4%BB%B6%E5%88%B0%E4%B8%8A%E4%B8%80%E4%B8%AA%E6%8F%90%E4%BA%A4%E7%89%88%E6%9C%AC"><span class="nav-number">2.</span> <span class="nav-text">git恢复某个文件到上一个提交版本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git%E5%90%88%E5%B9%B6%E7%89%B9%E5%AE%9A%E7%9A%84commit"><span class="nav-number">3.</span> <span class="nav-text">git合并特定的commit</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git%E8%B4%AE%E8%97%8F%E5%8A%9F%E8%83%BD%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="nav-number">4.</span> <span class="nav-text">git贮藏功能使用场景</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git%E8%B4%AE%E8%97%8F%E5%8A%9F%E8%83%BD%E4%BD%BF%E7%94%A8%E6%B5%81%E7%A8%8B%EF%BC%88git-shell%EF%BC%89"><span class="nav-number">5.</span> <span class="nav-text">git贮藏功能使用流程（git shell）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git%E6%9C%80%E5%9F%BA%E6%9C%AC%E7%9A%846%E4%B8%AA%E6%93%8D%E4%BD%9C"><span class="nav-number">6.</span> <span class="nav-text">git最基本的6个操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%AF%B4%E8%AF%B4git%E7%9A%84%E5%88%86%E6%94%AF-%E5%90%88%E5%B9%B6%E4%B8%8E%E5%86%B2%E7%AA%81"><span class="nav-number">7.</span> <span class="nav-text">说说git的分支,合并与冲突</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%AF%B4%E8%AF%B4git%E5%85%AC%E5%8F%B8%E5%A4%9A%E4%BA%BA%E5%8D%8F%E4%BD%9C%E4%B8%8E%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%E5%A4%9A%E4%BA%BA%E5%8D%8F%E4%BD%9C"><span class="nav-number">8.</span> <span class="nav-text">说说git公司多人协作与开源项目多人协作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8C%BA%E5%88%ABfork-%E5%A4%8D%E5%88%BB-%E4%B8%8Eclone-%E5%85%8B%E9%9A%86"><span class="nav-number">9.</span> <span class="nav-text">区别fork(复刻)与clone(克隆)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8C%BA%E5%88%ABgit%E7%9A%84pull%E4%B8%8Efetch"><span class="nav-number">10.</span> <span class="nav-text">区别git的pull与fetch</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#git%E7%9A%84%E5%87%A0%E4%B8%AA%E4%B8%BB%E8%A6%81%E5%88%86%E6%94%AF"><span class="nav-number">11.</span> <span class="nav-text">git的几个主要分支</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">郭泽</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">51</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gz1234.gitee.io/2021/12/12/git/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="郭泽">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郭泽">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="git常用命令 | 郭泽">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          git常用命令
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-12-12 22:37:05" itemprop="dateCreated datePublished" datetime="2021-12-12T22:37:05+08:00">2021-12-12</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-03-22 09:11:36" itemprop="dateModified" datetime="2023-03-22T09:11:36+08:00">2023-03-22</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/git/" itemprop="url" rel="index"><span itemprop="name">git</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="git-命令"><a href="#git-命令" class="headerlink" title="git 命令"></a>git 命令</h2><table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>git init</td>
<td>git仓库初始化</td>
</tr>
<tr>
<td>git clone <giturl></giturl></td>
<td>复制现有仓库</td>
</tr>
<tr>
<td>git status</td>
<td>查看git仓库状态</td>
</tr>
<tr>
<td>git log</td>
<td>查看commit列表记录<br>–oneline  浓缩成一行一条记录<br>–stat         输出文件改动的统计数据<br>-p               输出改动文件的具体内容<br>-w              忽略空格的修改和-p一起用</td>
</tr>
<tr>
<td>git show <SHA></SHA></td>
<td>查看commit具体记录，也有–stat,-p,-w的配置</td>
</tr>
<tr>
<td>git add .</td>
<td>将工作区全部改动提交到暂存区</td>
</tr>
<tr>
<td>git commit</td>
<td>将暂存区改动提交到版本库<br>-m             添加本次改动的title<br>–amend  更改最后一个commit包括缓存区改动和改动的title</td>
</tr>
<tr>
<td>git diff</td>
<td>查看工作区修改</td>
</tr>
<tr>
<td>git tag -a <tagname></tagname></td>
<td>设置commit标签<br>-a               设置标签名<br>-d               删除标签名</td>
</tr>
<tr>
<td>git checkout <Branch></Branch></td>
<td>切换分支</td>
</tr>
<tr>
<td>git checkout -b &lt;本地分支名&gt; &lt;远程分支名&gt;</td>
<td>创建本地分支并关联远程分支</td>
</tr>
<tr>
<td>git checkout -t &lt;远程分支名&gt;</td>
<td>创建本地分支并自动关联远程分支，本地分支名和远程分支一致</td>
</tr>
<tr>
<td>git checkout .</td>
<td>撤销工作区的修改(只能撤销在已有文件上的修改)</td>
</tr>
<tr>
<td>git restore .</td>
<td>撤销工作区的修改</td>
</tr>
<tr>
<td>git clean -df</td>
<td>删除工作区新增的文件</td>
</tr>
<tr>
<td>git clean -xdf</td>
<td>删除工作区新增的文件和文件夹</td>
</tr>
<tr>
<td>git merge <branch></branch></td>
<td>合并分支</td>
</tr>
<tr>
<td>git revert <sha></sha></td>
<td>还原，并向前提交一个新的commit来保存还原的内容</td>
</tr>
<tr>
<td>git reset</td>
<td>重置，会删除commit，慎用<br>–hard  会清空工作目录和暂存区的改动<br>–soft    则会保留工作目录的内容，并把因为保留工作目录内容所带来的新的文件差异放进暂存区。<br>–mixed  默认行为，保留工作目录，并且清空暂存区<br>HEAD^表示当前版本<br>HEAD^^上上一个版本<br>HEAD~2上上一个版本</td>
</tr>
<tr>
<td>git reset HEAD^</td>
<td>使用场景：已使用git add . 命令，未使用git commit -m ‘’命令</td>
</tr>
<tr>
<td>git reset –soft HEAD^</td>
<td>使用场景：已使用git commit -m ‘’命令，撤销之后会把修改放在暂存区</td>
</tr>
<tr>
<td>git reset  –hard  HEAD^</td>
<td>使用场景：已使用git commit -m，撤销之后不会保留任何更改，比如打包之后提交了commit，想重新打包，则可以使用</td>
</tr>
<tr>
<td>git reset HEAD</td>
<td>撤销缓存区的修改，撤销上一次add</td>
</tr>
<tr>
<td>git remote -v</td>
<td>远程库查询</td>
</tr>
<tr>
<td>git remote add origin <url></url></td>
<td>添加远程库</td>
</tr>
<tr>
<td>git remote remove <origin></origin></td>
<td>删除远程库</td>
</tr>
<tr>
<td>git push origin –delete <branch></branch></td>
<td>删除远程分支</td>
</tr>
<tr>
<td>git push &lt;分支名&gt;</td>
<td>推送本地分支到远程分支</td>
</tr>
<tr>
<td>git push origin –delete <branch></branch></td>
<td>删除远程分支</td>
</tr>
<tr>
<td>git push origin –delete &lt;分支名&gt;</td>
<td>删除远程分支</td>
</tr>
<tr>
<td>git config –global core.autocrlf false</td>
<td>禁用换行方式转换</td>
</tr>
<tr>
<td>git push -u origin master</td>
<td>跟踪远程分支</td>
</tr>
<tr>
<td>git push -f</td>
<td>强制推送</td>
</tr>
<tr>
<td>git pull &lt;分支名&gt;</td>
<td>拉取远程分支到本地分支</td>
</tr>
<tr>
<td>git pull -u origin dev</td>
<td>拉取远程分支</td>
</tr>
<tr>
<td>git branch</td>
<td>列出所有分支(仅本地分支)</td>
</tr>
<tr>
<td>git branch -a</td>
<td>列出所有分支(包括远程分支)</td>
</tr>
<tr>
<td>git branch -d &lt;分支名&gt;</td>
<td>删除分支</td>
</tr>
<tr>
<td>git branch -D &lt;分支名&gt;</td>
<td>强行删除分支</td>
</tr>
<tr>
<td>git branch &lt;分支名&gt;</td>
<td>创建新分支</td>
</tr>
<tr>
<td>git branch -m &lt;旧分支名&gt;  &lt;新分支名&gt;</td>
<td>重命名分支</td>
</tr>
<tr>
<td>git branch -vv</td>
<td>查看本地分支与远程分支的关联分支</td>
</tr>
<tr>
<td>git clean -n</td>
<td>显示将要删除的新增文件，不显示新增的文件夹及包含的文件</td>
</tr>
<tr>
<td>git clean -f</td>
<td>删除新增文件，不删除新增的文件夹及包含的文件</td>
</tr>
<tr>
<td>git clean -df</td>
<td>删除新增文件以及新增的文件夹和其包含的文件</td>
</tr>
<tr>
<td>git fetch &lt;分支名&gt;</td>
<td>获取远端的更新，但不进行合并</td>
</tr>
<tr>
<td>git config –global core.quotepath false</td>
<td>git中文显示数字的问题</td>
</tr>
<tr>
<td>git log –pretty=oneline 文件名 （文件名是文件路径+文件名，输入完整）</td>
<td>git查看某个文件的提交历史</td>
</tr>
<tr>
<td>git show 某次commit 的哈希值  文件名</td>
<td>git查看某个文件某次提交的修改</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td>git config –global gui.encoding utf-8</td>
<td></td>
</tr>
<tr>
<td>git config –global i18n.commit.encoding utf-8</td>
<td></td>
</tr>
<tr>
<td>git config –global i18n.logoutputencoding utf-8</td>
<td></td>
</tr>
<tr>
<td>$env:LESSCHARSET=’utf-8’</td>
<td>解决 powerShell git log显示中文的问题</td>
</tr>
<tr>
<td>git config –global core.autocrlf false</td>
<td>git提示LF CRLF转换的警告</td>
</tr>
<tr>
<td>git blame -L <lineStart>,<lineEnd> &lt;path+fileName&gt;</lineEnd></lineStart></td>
<td>git查看部分行的历史改动记录</td>
</tr>
<tr>
<td>git config –global core.quotepath false</td>
<td>解决中文显示成数字的bug</td>
</tr>
<tr>
<td>GIT_MERGE_AUTOEDIT=no      export GIT_MERGE_AUTOEDIT</td>
<td>用来解决git bash 合并时自动打开VIM编辑器的问题</td>
</tr>
<tr>
<td>git blame</td>
<td>找到具体代码的责任人</td>
</tr>
</tbody></table>
<a id="more"></a>

<h2 id="git恢复某个文件到上一个提交版本"><a href="#git恢复某个文件到上一个提交版本" class="headerlink" title="git恢复某个文件到上一个提交版本"></a>git恢复某个文件到上一个提交版本</h2><p>git提交了比较多的文件到远程，但是在合并时发现其中有一个文件合并有冲突或者某个原因不想修改该文件了，那就需要单独把这个文件回退到上一个提交版本状态。方法如下：</p>
<ul>
<li><p>首先查看一下该文件的commit记录：git log 文件，例如 git log src/index.java</p>
</li>
<li><p>找到需要提交到上一个版本的commit号，然后checkout该文件的上一版本，输入下面的指令：</p>
<p> git checkout [commit id] 文件，例如 git checkout a57fb4b474888f0db4cba18de2180496 src/index.java</p>
</li>
<li><p>然后将checkout的版本提交到本地</p>
<p> git commit -m “回退到上一版本”</p>
</li>
<li><p>最后将改变提交到分支远程：<br> git push origin 分支名<br> 这样此文件本地和远程都是上一版本内容</p>
</li>
</ul>
<h2 id="git合并特定的commit"><a href="#git合并特定的commit" class="headerlink" title="git合并特定的commit"></a>git合并特定的commit</h2><p>多版本并行开发的时候一般都是一个版本一个分支，验收通过上线的时候将该分支直接合并到生产环境中</p>
<p>但是有的时候我们只想合并某一个或几个commit，来满足业务的需求(这个功能今晚上，其他可以晚点上)</p>
<p>使用git cherry-pick <commit id> 合并特定的commit，操作对象是commit而非分支</commit></p>
<p>commit id 可以是来自任何分支，他不关心分支，因为commit id 总是唯一的</p>
<p>使用git log来查看commit id，可以是完整的commit id，也可以是id前6位</p>
<p>实际演示</p>
<p>本地有两个分支，master和uat，我们将uat分支的commitid=9494f1的记录合并到master上，而不合并整个分支</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b uat  &#x2F;&#x2F; 创建uat分支</span><br><span class="line">... &#x2F;&#x2F; 修改两次，提交两次commit</span><br><span class="line">git log   &#x2F;&#x2F; 记录两次修改的commit   第一次修改为9494f1 第二次修改为d3450d</span><br><span class="line">git checkout master &#x2F;&#x2F; 切回master分支</span><br><span class="line">git cherry-pick 9494f1  &#x2F;&#x2F; 合并commit</span><br><span class="line">git log &#x2F;&#x2F; 发现commit已合并</span><br></pre></td></tr></table></figure>

<h2 id="git贮藏功能使用场景"><a href="#git贮藏功能使用场景" class="headerlink" title="git贮藏功能使用场景"></a>git贮藏功能使用场景</h2><p>1、如果你某个分支开发过程中，这个分支的内容是要在本月月底上线的，但是生产上已经出现了一个重大bug，需要你立马去修复。你在分支开发的内容已经开发一部分了，工作区有内容是不能切换分支的，这个时候就可以用到贮藏的功能了</p>
<h2 id="git贮藏功能使用流程（git-shell）"><a href="#git贮藏功能使用流程（git-shell）" class="headerlink" title="git贮藏功能使用流程（git shell）"></a>git贮藏功能使用流程（git shell）</h2><ul>
<li><p>贮藏<br> git stash：将当前工作贮藏<br> git stash save  “描述内容”:给每个stash加一个message</p>
</li>
<li><p>查看贮藏列表<br> git stash list：查看贮藏的列表</p>
</li>
<li><p>应用<br> git stash apply:将缓存堆栈中的第一个stash多次应用到工作目录中，但并不删除stash拷贝<br> git stash apply stash@{0} ：恢复贮藏序列为0的贮藏中的工作内容,但并不删除stash拷贝</p>
</li>
<li><p>删除<br> git stash drop：将缓存堆栈中的第一个stash删除<br> git stash drop stash@{0}：删除贮藏序列为0的贮藏中的工作内容<br> git stash clear：删除所有缓存的stash</p>
</li>
<li><p>应用并删除</p>
<p> git stash pop：将缓存堆栈中的第一个stash删除，并将对应修改应用到当前的工作目录下。<br> git stash pop stash@{0}：将贮藏序列为0的贮藏中的工作内容应用并且删除</p>
</li>
<li><p>查看具体的修改记录<br> git stash show:查看缓存堆栈中的第一个stash的diff<br> git stash show stash@{0}:查看贮藏序列为0的stash的diff</p>
</li>
</ul>
<h2 id="git最基本的6个操作"><a href="#git最基本的6个操作" class="headerlink" title="git最基本的6个操作"></a>git最基本的6个操作</h2><ul>
<li>创建本地仓库<br> 创建.gitignore配置文件<br> git init<br> git add *<br> git commit -m “xxx”</li>
<li>创建远程仓库<br> New Repository<br> 指定名称<br> 创建</li>
<li>将本地仓库推送到远程仓库<br> git remote add origin <a target="_blank" rel="noopener" href="https://github.com/zxfjd3g/xxx.git">https://github.com/zxfjd3g/xxx.git</a> 关联远程仓库<br> git push origin master</li>
<li>如果本地有更新, 推送到远程<br> git add *<br> git commit -m “xxx”<br> git push origin master</li>
<li>如果远程有更新, 拉取到本地<br> git pull origin master<br> git fetch origin master:tmp</li>
<li>克隆远程仓库到本地<br> git clone <a target="_blank" rel="noopener" href="https://github.com/zxfjd3g/xxx.git">https://github.com/zxfjd3g/xxx.git</a><br> git checkout -b dev origin/dev</li>
</ul>
<h2 id="说说git的分支-合并与冲突"><a href="#说说git的分支-合并与冲突" class="headerlink" title="说说git的分支,合并与冲突"></a>说说git的分支,合并与冲突</h2><ul>
<li>分支是在开发主线之外编写你的代码完成特定工作而不影响开发主线</li>
<li>分支的操作:<br> 查看分支: git branch<br> 创建分支: git branch dev<br> 切换分支: git checkout dev<br> 比较分支: git diff master dev<br> 合并分支: git merge dev</li>
<li>冲突<br> 产生冲突: 当对2个分支进行合并时, 如果同一个文件的同一行内容不一样, 就会产生冲突<br> 解决冲突:<pre><code>修正合并后产生冲突的代码
git add *
git commit -m &quot;resolve conflict&quot;</code></pre>
</li>
</ul>
<h2 id="说说git公司多人协作与开源项目多人协作"><a href="#说说git公司多人协作与开源项目多人协作" class="headerlink" title="说说git公司多人协作与开源项目多人协作"></a>说说git公司多人协作与开源项目多人协作</h2><ul>
<li>公司多人协作: 先在github上创建组织(修改权限), 多个同事加入此组织, 在组织下创建项目, 成员都可以进行推送更新</li>
<li>开源多人协作: fork仓库到自己的账户下, 修改fork仓库的代码, 向原仓库发起一个pull request, 对方接收到请求后可以选择合并</li>
</ul>
<h2 id="区别fork-复刻-与clone-克隆"><a href="#区别fork-复刻-与clone-克隆" class="headerlink" title="区别fork(复刻)与clone(克隆)"></a>区别fork(复刻)与clone(克隆)</h2><ul>
<li>fork: 将别人的远程仓库完全复制为自己的远程仓库</li>
<li>clone:  将远程仓库(自己/别人)完全复制为本地仓库</li>
</ul>
<h2 id="区别git的pull与fetch"><a href="#区别git的pull与fetch" class="headerlink" title="区别git的pull与fetch"></a>区别git的pull与fetch</h2><ul>
<li>简单来说，git pull 是 git fetch + git merge</li>
<li>执行pull: 将远程仓库的更新拉取到本地后, 与本地仓库当前分支进行自动合并(可能会遇到冲突)</li>
<li>执行fetch: 将远程仓库的更新拉取到一个暂时的新分支上, 后面需要手动进行merge操作(可以先进行diff操作)</li>
</ul>
<h2 id="git的几个主要分支"><a href="#git的几个主要分支" class="headerlink" title="git的几个主要分支"></a>git的几个主要分支</h2><ul>
<li>master分支<br>主分支，产品的功能全部实现后，最终在master分支对外发布。</li>
<li>develop分支<br>开发分支，基于master分支克隆，产品的编码工作在此分支进行。</li>
<li>release分支<br>测试分支，基于delevop分支克隆，产品编码工作完成后，发布到本分支测试，测试过程中发现的小bug直接在本分支进行修复，修复完成后合并到develop分支。本分支属于临时分支，目的实现后可删除分支。</li>
<li>bugfix/hotfix分支<br>Bug修复分支，基于master分支或发布的里程碑Tag克隆，主要用于修复对外发布的分支，收到客户的Bug反馈后，在此分支进行修复，修复完毕后分别合并到develop分支和master分支。本分支属于临时分支，目的实现后可删除分支。</li>
<li>feature分支<br>功能特征分支，基于develop分支克隆，主要用于多人协助开发场景或探索性功能验证场景，功能开发完毕后合并到develop分支。feature分支可创建多个，属于临时分支，目的实现后可删除分支。</li>
</ul>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/git%E5%91%BD%E4%BB%A4/" rel="tag"># git命令</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/05/14/browersRander/" rel="prev" title="浏览器原理">
                  <i class="fa fa-chevron-left"></i> 浏览器原理
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/03/13/%E8%BF%90%E7%AE%97%E7%AC%A6%E4%BC%98%E5%85%88%E7%BA%A7/" rel="next" title="运算符优先级">
                  运算符优先级 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">郭泽</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.8/pdfobject.min.js","integrity":"sha256-tu9j5pBilBQrWSDePOOajCUdz6hWsid/lBNzK4KgEPM="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>

  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.1.3/mermaid.min.js","integrity":"sha256-TIYL00Rhw/8WaoUhYTLX9SKIEFdXxg+yMWSLVUbhiLg="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>

  <script src="/js/third-party/fancybox.js"></script>


  





</body>
</html>
